package com.fili.gui;

import com.fili.gui.components.PanelGrid;
import com.fili.gui.components.TableView;
import com.fili.gui.view.DataView;
import com.fili.gui.view.ISPMView;
import com.fili.core.CentralNode;
import com.fili.JDryMain;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

/**
 * Created by Claudiu-Florin FILIMON
 * Date: Oct 14, 2006
 * Time: 11:48:26 AM
 */
public class JDryMainFrame extends JFrame
{
    SystemTray tray = SystemTray.getSystemTray();
    TrayIcon icon;
    ActionsEngine actionsEngine;

    private JMenuBar menuBar;
    private JTabbedPane tabbedPane;

    private CentralNode centralNode;



    public JDryMainFrame()
    {
        super("Uscator");
        actionsEngine = new ActionsEngine(this);
        JDryMainFrameActions.setupActions(actionsEngine);

        setupIcon();

        setSize(800, 600);
        setExtendedState(MAXIMIZED_BOTH);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        menuBar = new JMenuBar();
        JMenu menu = new JMenu("Fisier");
        menu.add(actionsEngine.getAction(JDryMainFrameActions.ACTION_EXIT));
        menuBar.add(menu);

        setJMenuBar(menuBar);

        DataView[] dView = new DataView[]{
                new ISPMView(),
                new ISPMView(),
                new ISPMView(),
                new ISPMView(),
                new ISPMView(),
                new ISPMView(),
                new ISPMView(),
                new ISPMView()
        };

        PanelGrid pnl = new PanelGrid(4, 2, dView);

        centralNode = new CentralNode(dView.length);
        centralNode.linkTo(dView);

        tabbedPane = new JTabbedPane();
        tabbedPane.addTab("Uscatoare", new JScrollPane(pnl));
        tabbedPane.addTab("Date", new JScrollPane(new TableView(null)));
        tabbedPane.addTab("Grafice", new JScrollPane());
        add(tabbedPane);

        showWindow();
    }

    private void setupIcon()
    {
        PopupMenu pm = new PopupMenu();

        MenuItem mi = new MenuItem((String) actionsEngine.getAction(JDryMainFrameActions.ACTION_SHOW).getValue(Action.SHORT_DESCRIPTION));
        pm.add(mi);
        mi.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                do_ACTION_SHOW();
            }
        });
        mi = new MenuItem((String) actionsEngine.getAction(JDryMainFrameActions.ACTION_EXIT).getValue(Action.SHORT_DESCRIPTION));
        pm.add(mi);
        mi.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                do_ACTION_EXIT();
            }
        });

        Image ico = new ImageIcon("res/img/icon.png").getImage();
        icon = new TrayIcon(ico, "Test", pm);
        try
        {
            tray.add(icon);
        }
        catch (AWTException e)
        {
            e.printStackTrace();
        }
        icon.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                showWindow();
            }
        });
        System.out.println("Added!");


        addWindowListener(new WindowListener()
        {
            public void windowOpened(WindowEvent e)
            {
            }

            public void windowClosing(WindowEvent e)
            {
//                hideWindow();
                JDryMain.params.saveParams();
                System.exit(1);
            }

            public void windowClosed(WindowEvent e)
            {
            }

            public void windowIconified(WindowEvent e)
            {
                hideWindow();
            }

            public void windowDeiconified(WindowEvent e)
            {
            }

            public void windowActivated(WindowEvent e)
            {
            }

            public void windowDeactivated(WindowEvent e)
            {
            }
        });
    }

    private void showWindow()
    {
        setVisible(true);
        setState(JFrame.NORMAL);
        tray.remove(icon);
    }

    private void hideWindow()
    {
        setVisible(false);
        try
        {
            tray.add(icon);
        }
        catch (AWTException e)
        {
            e.printStackTrace();
        }
    }


    public void do_ACTION_SHOW()
    {
        showWindow();
    }

    public void do_ACTION_HIDE()
    {
        hideWindow();
    }

    public void do_ACTION_EXIT()
    {
        JDryMain.params.saveParams();
        System.exit(0);
    }
}
